<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Node(value) {
            this.value = value;
            this.left = null;
            this.right = null;
            this.next = null;
        }
        var a = new Node(1)
        var b = new Node(null)
        var c = new Node(2)
        var d = new Node(3)
        var e = new Node(4)
        var f = new Node(5)
        var g = new Node(6)
        a.left = b
        a.right = c
        b.left = d
        b.right = e
        // c.left = f
        // c.right = g
        var minDepth = function (root) {
            if (root === null) {
                return 0;
            }
            let res = [], queue = [];
            // 初始化深度 至少有一层
            let depth = 1
            queue.push(root);
            while (queue.length !== 0) {
                let length = queue.length;
                for (let i = 0; i < length; i++) {
                    let node = queue.shift();
                    if (!node.left && !node.right) {
                        // 叶子结点 返回深度
                        return depth
                    }
                    node.left && queue.push(node.left);
                    node.right && queue.push(node.right);
                }
                // 遍历完一层 深度+1
                depth++
            }
        };
        console.log(minDepth(a));
    </script>
</body>

</html>